﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.DataAccess;
using WarehouseManager.Model;

namespace WarehouseManager.View
{
    public partial class FormHanghoa : Form
    {
        private HanghoaAccess hanghoaAccess = new HanghoaAccess();
        private DataTable dataTable = new DataTable();
        public FormHanghoa()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void FormHanghoa_Load(object sender, EventArgs e)
        {
            dataTable=hanghoaAccess.layToanbohanghoa();
            dataGridView1.DataSource = dataTable;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMahanghoa.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            txtTenhanghoa.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtDongia.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtDonvitinh.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtSoluong.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtNhasx.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtMahanghoa.ReadOnly = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMahanghoa.ReadOnly = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnThem.Enabled = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            HangHoa hh = new HangHoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), txtNhasx.Text);
            if (hanghoaAccess.themhanghoa(hh) == 0) { MessageBox.Show("Mã học sinh đã tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã thêm thành công !");

            }
            dataGridView1.DataSource = hanghoaAccess.layToanbohanghoa();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            HangHoa hh = new HangHoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), txtNhasx.Text);
            if (hanghoaAccess.suahanghoa(hh) == 0) { MessageBox.Show("Mã học sinh không tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã sửa thành công !");

            }
            dataGridView1.DataSource = hanghoaAccess.layToanbohanghoa();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            HangHoa hh = new HangHoa(txtMahanghoa.Text, txtTenhanghoa.Text, float.Parse(txtDongia.Text), txtDonvitinh.Text, int.Parse(txtSoluong.Text), txtNhasx.Text);
            if (hanghoaAccess.xoahanghoa(hh) == 0) { MessageBox.Show("Mã học sinh không tồn tại !"); }
            else
            {
                MessageBox.Show("Bạn đã xóa thành công !");

            }
            dataGridView1.DataSource = hanghoaAccess.layToanbohanghoa();
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            DataView dataView = new DataView();
            if (cbbTimkiem.SelectedItem == cbbTimkiem.Items[0])
            {
                dataView = hanghoaAccess.timkiemMahh(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
            else
            {
                dataView = hanghoaAccess.timkiemTenhh(txtTimkiem.Text);
                dataGridView1.DataSource = dataView;
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

        }      
    }
}
